package com.hitao.util;

import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class PlistGenerate {
    public void createPlist(String strFilename, String strIpaUrl,String strIpaIconUrl,String strIpaBundle,String strIpaVersion, String strIpaTitle, String strOutputPath) throws IOException {
        //plist文件存储目录
        String filePath = strOutputPath + strFilename + ".plist";
        // 创建文档类型
        DocType docType = new DocType("plist");
        docType.setPublicID("-//Apple//DTD PLIST 1.0//EN");
        docType.setSystemID("http://www.apple.com/DTDs/PropertyList-1.0.dtd");
        // 创建根节点 plist
        Element root = new Element("plist");
        root.setAttribute("version", "1.0");
        //
        Element rootDict = new Element("dict");
        rootDict.addContent(new Element("key").setText("items"));
        Element rootDictArray = new Element("array");
        Element rootDictArrayDict = new Element("dict");
        rootDictArrayDict.addContent(new Element("key").setText("assets"));
        Element rootDictArrayDictArray = new Element("array");
        Element rootDictArrayDictArrayDict1 = new Element("dict");
        rootDictArrayDictArrayDict1.addContent(new Element("key").setText("kind"));
        rootDictArrayDictArrayDict1.addContent(new Element("string").setText("software-package"));
        rootDictArrayDictArrayDict1.addContent(new Element("key").setText("url"));
        rootDictArrayDictArrayDict1.addContent(new Element("string").setText(strIpaUrl));
        Element rootDictArrayDictArrayDict2 = new Element("dict");
        rootDictArrayDictArrayDict2.addContent(new Element("key").setText("kind"));
        rootDictArrayDictArrayDict2.addContent(new Element("string").setText("display-image"));
        rootDictArrayDictArrayDict2.addContent(new Element("key").setText("needs-shine"));
        rootDictArrayDictArrayDict2.addContent(new Element("true"));
        rootDictArrayDictArrayDict2.addContent(new Element("key").setText("url"));
        rootDictArrayDictArrayDict2.addContent(new Element("string").setText(strIpaIconUrl));
        rootDictArrayDictArray.addContent(rootDictArrayDictArrayDict1);
        rootDictArrayDictArray.addContent(rootDictArrayDictArrayDict2);
        rootDictArrayDict.addContent(rootDictArrayDictArray);
        rootDictArrayDict.addContent(new Element("key").setText("metadata"));
        Element rootDictArrayDictDict = new Element("dict");
        rootDictArrayDictDict.addContent(new Element("key").setText("bundle-identifier"));
        rootDictArrayDictDict.addContent(new Element("string").setText(strIpaBundle));
        rootDictArrayDictDict.addContent(new Element("key").setText("bundle-version"));
        rootDictArrayDictDict.addContent(new Element("string").setText(strIpaVersion));
        rootDictArrayDictDict.addContent(new Element("key").setText("kind"));
        rootDictArrayDictDict.addContent(new Element("string").setText("software"));
        rootDictArrayDictDict.addContent(new Element("key").setText("title"));
        rootDictArrayDictDict.addContent(new Element("string").setText(strIpaTitle));
        rootDictArrayDict.addContent(rootDictArrayDictDict);
        rootDictArray.addContent(rootDictArrayDict);
        rootDict.addContent(rootDictArray);
        root.addContent(rootDict);
        // 根节点添加到文档中;
        Document Doc = new Document(root, docType);
        Format format = Format.getPrettyFormat();
        XMLOutputter XMLOut = new XMLOutputter(format);
        // 输出 user.xml 文件；
        FileOutputStream fos = new FileOutputStream(new File(filePath));
        XMLOut.output(Doc, fos);
    }
}
